#!/bin/bash

binlogdir=/var/lib/mysql/


lastbinlogfile=$1
transid=$2

findidstr='SET INSERT_ID='$transid'/*!*/;'

fileindex=`echo ${lastbinlogfile:0-6}`
let filei=$fileindex
fileprefix=`echo ${lastbinlogfile%.*}`
for((i=$filei;i>0;i--));  
do
	binlogfile=$fileprefix"."`printf "%06d" $i`
	realbinlogfile=$binlogdir$binlogfile
	#echo "$binlogfile "`date`

	mysqlbinlog $realbinlogfile > ./tmpbin

	insertpos=`cat ./tmpbin|grep -n '^INSERT INTO'|tac|grep '_tranid'|awk -F':' '{print $1}'`
	for pos in $insertpos; do
		idpos=`expr $pos - 3`
		#echo "$pos,$idpos "`date`
		idstr=`cat ./tmpbin|sed -n ${idpos}p`
		#echo "findidstr=$findidstr, $idstr"
		if [ "$findidstr" == "$idstr" ]; then
			#echo "find it"
			nextpos=`expr $pos + 2`
			nextposstr=`cat ./tmpbin|sed -n ${nextpos}p`
			nextposval=`echo ${nextposstr##* }`
			#echo "nextposval=$nextposval "`date`
			echo "MASTER_LOG_FILE='$binlogfile', MASTER_LOG_POS=$nextposval"
			exit
		#else
			#echo "not find it"
		fi
	done

done  
echo -1
